package com.physics.mps.common.config;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author mcsoft
 * @date 2023/8/28 14:51
 */
@Log4j2
@RequiredArgsConstructor
@Configuration
@ConfigurationPropertiesScan({"com.physics.mps.common.config.properties"})
public class CustomConfig {

    /**
     * 运行odb转换vtu线程池
     *
     * @return
     */
    @Bean(name = "runOdbToVtuCmdExecutorService", destroyMethod = "shutdown")
    public ExecutorService runOdbToVtuCmdExecutorService() {
        log.info("create runOdbToVtuCmdExecutorService");
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNamePrefix("runOdbToVtu-pool-").build();
        return new ThreadPoolExecutor(5, 5, 0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(100),
                namedThreadFactory, new ThreadPoolExecutor.DiscardOldestPolicy());
    }

    /**
     * 读取odb转换vtu cmd 运行流输出线程池
     *
     * @return
     */
    @Bean(name = "readOdbToVtuCmdExecutorService", destroyMethod = "shutdown")
    public ExecutorService readOdbToVtuCmdExecutorService() {
        log.info("create readOdbToVtuCmdExecutorService");
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNamePrefix("readOdbToVtu-pool-").build();
        return new ThreadPoolExecutor(5, 5, 0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(100),
                namedThreadFactory, new ThreadPoolExecutor.DiscardOldestPolicy());
    }

    /**
     * 读取运行流输出线程池
     *
     * @return
     */
    @Bean(name = "readScriptExecutorService", destroyMethod = "shutdown")
    public ExecutorService readScriptExecutorService() {
        log.info("create readScriptExecutorService");
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNamePrefix("readScript-pool-").build();
        return new ThreadPoolExecutor(5, 5, 0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(100),
                namedThreadFactory, new ThreadPoolExecutor.DiscardOldestPolicy());
    }

    /**
     * 执行脚本线程池
     *
     * @return
     */
    @Bean(name = "executeScriptExecutorService", destroyMethod = "shutdown")
    public ExecutorService executeScriptExecutorService() {
        log.info("create executeScriptExecutorService");
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNamePrefix("executeScript-pool-").build();
        return new ThreadPoolExecutor(5, 5, 0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(100),
                namedThreadFactory, new ThreadPoolExecutor.DiscardOldestPolicy());
    }
}
